package Top200;

import common.ListNode;

/**
 * @author zhangmin
 * @create 2022-02-19 10:11
 */
public class reverseKGroup25 {
    //反转[a,b)的节点
    ListNode reverse(ListNode a,ListNode b){
        ListNode cur=a,next=a,pre=null;
        while (cur!=b){
            next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head==null) return null;
        ListNode a=head,b=head;
        for (int i = 0; i < k; i++) {
            if (b==null){
                return head;
            }
            b=b.next;
        }
        ListNode newhead=reverse(a,b);
        a.next=reverseKGroup(b,k);
        return newhead;
    }
}
